package com.et.launch.util

import android.util.Log
import com.et.launch.BuildConfig
import com.et.launch.globalUuid
import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.util.FtpUtil
import com.et.util.httpPost
import com.et.util.httpSign
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.schedulers.Schedulers
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.HashMap


fun httpUploadLogResult(date: String, response: FtpUtil.Response) {
    // Document(https://www.yuque.com/docs/share/8fa56f4f-3c04-44d6-8069-b1333aef009a?# 《后台接口文档》)
    val map = HashMap<String, Any>()
    map["avm"] = globalUuid
    map["datetime"] =
        SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA).format(System.currentTimeMillis())
    map["myappid"] = BuildConfig.http_myappid
    map["flag"] = if (response.success) "0" else "1"
    map["date"] = date
    httpSign(map, BuildConfig.http_key, false)
    map["msg"] = response.message
    Single.just(map)
        .map {
            httpPost(BuildConfig.http_server_log_upload_result, map)
        }
        .subscribeOn(Schedulers.single())
        .observeOn(Schedulers.single())
        .subscribe({
            ELog.d(TAGS.launch, "launch:log upload result $it")
            if (it.success) {
                ELog.d(TAGS.launch, "launch:upload log result finish")
            } else {
                ELog.d(TAGS.launch, "launch:upload log result failure")
            }
        }, {
            ELog.d(TAGS.launch, "launch:upload log result error")
            if (BuildConfig.DEBUG) Log.e(TAGS.launch, "launch>>log>>upload:", it)
        })
}